/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski   *
 *   g.latarowski@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef MARKER_H
#define MARKER_H


// Qt
#include <QtGui>

// lib
#include "gxmlelement.h"

// local
#include "graph.h"


// ----------------------------- GMarker{} -----------------------------------
/// \brief Represents marker for markers regions editor.
///
/// Marker is moveable on the scene and is rotateable through hook manipulating.
/// Marker is represented by a line but a markers region by a quadrangle.
// ---------------------------------------------------------------------------
class GMarker : public GGraph, public GXmlElement
{
	Q_OBJECT
	
public:

	// ====================================================
	// constructors 

	/// \brief Creates marker.
	///
	/// Calls GGraph constructor with \a pItem as parent.
	/// Creates markers nodes, sets markers color to white.
	GMarker( QGraphicsItem * pItem = 0 );


	/// \brief Creates marker.
	///
	/// Calls GGraph contructor with \a pItem as parent.
	/// Creates hooks, sets starting hook position to \a hook1Pos
	/// and sets ending hook position to \a hook2Pos, sets default
	/// color to white.
	GMarker( const QPointF hook1Pos, const QPointF hook2Pos,
			 QGraphicsItem * pItem = 0 );

	/// \brief Creates marker.
	///
	/// Calls GGraph constructor with \a pItem as parent.
	/// Creates nodes and sets markers color to \a color.
	GMarker( const QColor color = QColor( Qt::white ), QGraphicsItem * pItem = 0 );

	/// \brief Creates marker.
	///
	/// Calls GGraph constructor with \a pItem as parent.
	/// Creates hook, sets starting hook position to \a hook1Pos and sets
	/// ending hook position to \a hook2Pos, sets color to color.
	GMarker( const QPointF hook1Pos, const QPointF hook2Pos,
			 const QColor color = QColor( Qt::white ), QGraphicsItem * pItem = 0 );

	/// \brief Deletes an item. Deletes hooks.
	~GMarker();

	
	// ====================================================
	// xml element

	virtual QDomElement toXml( QDomDocument & doc )const;
	
	virtual void fromXml( const QDomElement & element );

	
	// ====================================================
	// Markers specific 

	/// \brief Sets markers \e _color to \a color.
	void setColor( const QColor & color ) { _color = color; }

	/// \brief Returns markers color.
	QColor color() const { return _color; }

	/// \brief Sets markers hoooks positions to \a pos1 and \a pos2.
	///
	/// Starting hook is positioned to \a pos1 and ending hook to \a pos2.
	void setHooks( const QPointF & pos1, const QPointF & pos2 );

	/// \brief Returns hooks positions as a polygon.
	///
	/// Starting hook is polygon[0] and ending hook is polygon[1].
	QPolygonF hooks()const;

	/// \brief Sets markers zValue to \e z and hooks to z+0.01
	void setZValue( const qreal z );

	/// \brief Adjusts markers position.
	///
	/// Calculates markers quadrangle
	void adjust();

	/// \brief Returns a path describing marker region.
	QPainterPath markerRegion()const { return _coloredPath; }
	
	// ====================================================
	// QGraphics specific 

	/// \brief Returns markers bounding rect. 
	virtual QRectF boundingRect() const;

	/// \brief Returns markers shape as a strip. 
	virtual QPainterPath shape() const;

	/// \brief Paints marker as a colored quadrangle.
	virtual void paint( QPainter * pPainter,
						const QStyleOptionGraphicsItem * pOption,
						QWidget * pWidget );
	
protected:
	/// \brief Moves markers hooks when marker is moved. 
	virtual void mouseMoveEvent( QGraphicsSceneMouseEvent * event );
	
private:
	QColor _color;              ///< Stores markers color 
	
	QRectF   _boundingRect;     ///< Stores markers rect, same as its scene rect 
	
	GNode  * _pHook1;           ///< Pointer to markers starting hook
	GNode  * _pHook2;           ///< Pointer to markers endign hook 
	
	QLineF       _markerLine;   ///< Stores markers line, used to paint 
	QPainterPath _coloredPath;  ///< Stores markers colored path, used to paint quadrangle
	

	/// Creates hooks with default parameters.
	void createHooks();

	/// Rotates nodes if marker is rotated. 
	void rotateNodes();

}; // class !GMarker
// ---------------------------------------------------------------------------



#endif // !MARKER_H

//eof
